package com.hjm.ui.shopcart.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.hjm.ui.R;
import com.hjm.ui.shopcart.ShopCartItem;
import com.hjm.ui.shopcart.entity.ShopCartVO;

import java.util.List;

public class ShopCartStoreGoodsAdapter extends RecyclerView.Adapter<ShopCartStoreGoodsAdapter.ViewHolder> {
    List<ShopCartVO> shopCartVOList;
    Context context;
    private ShopCartItem.Listener listener;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    public ShopCartStoreGoodsAdapter(Context context, List<ShopCartVO> shopCartVOList) {
        this.shopCartVOList = shopCartVOList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_cart_item_store_goods, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopCartVO shopCartVO = shopCartVOList.get(position);
        holder.goodsName.setText(shopCartVO.getGoodsName());
        holder.goodsType.setText(shopCartVO.getGoodsType());
        holder.goodsNum.setText(String.valueOf(shopCartVO.getGoodsNum()));
        holder.goodsPrice.setText(String.valueOf(shopCartVO.getPrice().floatValue()*shopCartVO.getGoodsNum()));
        holder.selectCheckBox.setChecked(shopCartVO.isGoodsSelect());
        holder.goodsImg.setImageBitmap(shopCartVO.getGoodsIconBitmap());

        // 点击商品图片和商品名称跳转到商品详情页
        holder.goodsImg.setOnClickListener((v) -> {
            listener.onItemClick(shopCartVO.getGoodsId());
        });
        holder.shopCartItemGoodsLineLayout.setOnClickListener((v) -> {
            listener.onItemClick(shopCartVO.getGoodsId());
        });

        holder.selectCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            private static final String TAG = "ShopCartStoreGoods";

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "onCheckedChanged: 复选 "+isChecked);
                shopCartVO.setGoodsSelect(isChecked);
                listener.selectChanged(isChecked, shopCartVO.getOrderId());
            }
        });

        holder.minusImageview.setOnClickListener((v) -> {
            int num = shopCartVO.getGoodsNum();
            if (num > 1) {
                num--;
                shopCartVO.setGoodsNum(num);
//                holder.setGoodsNum(num);
                listener.onNumChanged(num, shopCartVO.getOrderId());
            } else {
                listener.onNumChanged(0, shopCartVO.getOrderId());
            }
        });

        holder.addImageView.setOnClickListener((v) -> {
            int num = shopCartVO.getGoodsNum();
            if (shopCartVO.getGoodsNum()< 99) {
                num++;
                shopCartVO.setGoodsNum(num);
//                holder.setGoodsNum(num);
                listener.onNumChanged(num, shopCartVO.getOrderId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return shopCartVOList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView goodsName, goodsType, goodsNum, goodsPrice;
        CheckBox selectCheckBox;
        ImageView goodsImg, minusImageview, addImageView;
        LinearLayout shopCartItemGoodsLineLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsName = itemView.findViewById(R.id.text_view_shop_cart_item_name);
            goodsType = itemView.findViewById(R.id.text_view_shop_cart_item_type);
            goodsNum = itemView.findViewById(R.id.text_view_shop_cart_item_num);
            goodsPrice = itemView.findViewById(R.id.text_view_shop_cart_item_price);
            goodsImg = itemView.findViewById(R.id.image_view_shop_cart_item_img);
            minusImageview = itemView.findViewById(R.id.image_view_shop_cart_item_num_minus);
            addImageView = itemView.findViewById(R.id.image_view_shop_cart_item_num_add);
            selectCheckBox = itemView.findViewById(R.id.checkbox_shop_cart_item_select);
            shopCartItemGoodsLineLayout = itemView.findViewById(R.id.linear_layout_shop_cart_item_goods);
        }

        public void setGoodsNum(int num){
            goodsNum.setText(String.valueOf(num));
        }
    }

    public void setListener(ShopCartItem.Listener listener) {
        this.listener = listener;
    }
}
